package com.example.sangong.kafkacase;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

public class MyTimeStampInterceptor implements ProducerInterceptor<Integer, String> {

    //拦截数据
    @Override
    public ProducerRecord<Integer, String> onSend(ProducerRecord<Integer, String> record) {
        System.err.println("MyTimeStampInterceptor:onSend-执行");

        String newValue = System.currentTimeMillis() + "|" + record.value();

        return new ProducerRecord<Integer, String>(record.topic(), record.key(), newValue);
    }

    //当拦截器收到此条消息的ack时，会自动调用onAcknowledgement()
    @Override
    public void onAcknowledgement(RecordMetadata metadata, Exception exception) {
        System.err.println("MyTimeStampInterceptor:onAcknowledgement-执行");

    }

    // Producer关闭时，调用拦截器的close()
    @Override
    public void close() {

    }

    //读取Producer中的配置
    @Override
    public void configure(Map<String, ?> configs) {

    }
}